package cn.jji8.createX.function.wand;

import cn.jji8.createX.Main;
import cn.jji8.createX.function.wand.canPlaceBlock.WandFunction;
import cn.jji8.createX.function.wand.canPlaceBlock.WandFunctionBook;
import cn.jji8.createX.function.wand.canPlaceBlock.WandFunctionState;
import cn.jji8.createX.manipulate.Selector;
import cn.jji8.createX.manipulate.SelectorImplement;
import cn.jji8.patronSaint.quick.boxesMenu.CanSaveClickImplement;
import cn.jji8.patronSaint.quick.boxesMenu.CanSavedMenuManger;
import cn.jji8.patronSaint.quick.boxesMenu.ClickImplement;
import org.bukkit.Material;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Wand implements ClickImplement {
    //界面
    CanSavedMenuManger MenuManger;
    //替换的方块
    Material replaceMaterial = null;
    //填充控制器
    WandFunctionState wandFunctionState = null;
    //玩家使用工具的控制器
    SelectorImplement selectorImplement = e -> {
        ItemStack itemStack = e.getItem();
        //手上没东西，排除
        if(itemStack==null){
            return;
        }
        //手上不是小斧头，排除
        if(!itemStack.getType().equals(Material.WOODEN_AXE)){
            return;
        }
        //不是右键，排除
        if(!e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
            return;
        }
        //没有填充方块，不填
        if(replaceMaterial==null){
            return;
        }
        e.setCancelled(true);
        if(wandFunctionState!=null){
            wandFunctionState.setCancel(true);
        }
        //方块与替换方块相同
        if(e.getClickedBlock().getType().equals(replaceMaterial)){
            return;
        }
        //是基岩
        if(e.getClickedBlock().getType().equals(Material.BEDROCK)){
            return;
        }
        if(!WandFunctionBook.airReplace){
            if(replaceMaterial.equals(Material.AIR)){
                return;
            }
        }
        wandFunctionState = WandFunction.start(e.getPlayer(),e.getClickedBlock(),replaceMaterial,this);
    };
    //玩家的挖掘工具
    public ItemStack iMiningTools = new ItemStack(Material.AIR);

    //挖掘工具按钮
    public class MiningToolsButton implements CanSaveClickImplement{
        boolean open = false;
        @Override
        public void Save(YamlConfiguration yamlConfiguration) {
            yamlConfiguration.set("iMiningTools",iMiningTools);
        }
        @Override
        public void load(YamlConfiguration yamlConfiguration) {
            ItemStack itemStack1 = yamlConfiguration.getItemStack("iMiningTools");
            if(itemStack1!=null){
                iMiningTools = itemStack1;
            }
        }

        @Override
        public void close(Player player, Inventory menu, int position) {
            open =false;
        }

        @Override
        public void click(InventoryClickEvent e) {
            e.setCancelled(true);
            //获取玩家光标上的物品
            ItemStack Cursor = e.getCursor();
            if(Cursor==null){
                Cursor = new ItemStack(Material.AIR);
            }
            ItemStack itemStack2 = new ItemStack(Cursor);
            e.getWhoClicked().getOpenInventory().setCursor(iMiningTools);
            iMiningTools = itemStack2;
            Refresh();
        }
        @Override
        public void open(Player player, Inventory menu, int position) {
            open = true;
            Refresh(position);
        }
        public void Refresh(){
            if(open){
                Refresh(53);
                Refresh(45);
            }
        }
        public void Refresh(int position){
            if(iMiningTools.getType().equals(Material.AIR)){
                ItemStack itemStack = new ItemStack(Material.WOODEN_PICKAXE);
                ItemMeta itemMeta = itemStack.getItemMeta();
                assert itemMeta != null;
                itemMeta.setDisplayName("无挖掘工具");
                ArrayList<String> loer = new ArrayList<>();
                loer.add("§g(将使用此挖掘工具)");
                itemMeta.setLore(loer);
                itemStack.setItemMeta(itemMeta);
                Wand.this.MenuManger.setItem(position,itemStack);
                return;
            }
            ItemStack itemStack1 = new ItemStack(iMiningTools);
            ItemMeta itemMeta = itemStack1.getItemMeta();
            List<String> loer = itemMeta.getLore();
            if(loer==null){
                loer = new ArrayList<>();
            }
            loer.add("§g(将使用此挖掘工具)");
            itemMeta.setLore(loer);
            itemStack1.setItemMeta(itemMeta);
            Wand.this.MenuManger.setItem(position,itemStack1);
        }
    }
    public MiningToolsButton MiningToolsButton = new MiningToolsButton();
    /**
     * 构造函数
     * */
    public Wand(Player player) {
        MenuManger = new CanSavedMenuManger(new File(Main.getMain().getDataFolder(),"menu/Wand/"+player.getName()),6,"§g魔棒工具");

        //填充开启器
        ClickImplement clickImplement = new ClickImplement(){
            boolean ShowFilling = false;
            @Override
            public void open(Player player, Inventory menu, int position) {
                ShowFilling = false;
                SelectorImplement selectorImpleme = Selector.getPlayerSelectorimplement(player);
                if(selectorImpleme!=selectorImplement){
                    ItemStack itemStack = new ItemStack(Material.RED_WOOL);
                    ItemMeta itemMeta = itemStack.getItemMeta();
                    assert itemMeta != null;
                    itemMeta.setDisplayName("§d魔棒工具(未激活)");
                    ArrayList<String> loer = new ArrayList<>();
                    loer.add("§e点击激活魔棒工具");
                    loer.add("");
                    loer.add("--------------------------------");
                    loer.add("§g帮助:");
                    loer.add("§g1.激活魔棒工具             §a<<--当前步骤.");
                    loer.add("§g2.放置需要替换的方块");
                    loer.add("§g3.使用工具右键需要替换的方块");
                    itemMeta.setLore(loer);
                    itemStack.setItemMeta(itemMeta);
                    menu.setItem(position,itemStack);
                    return;
                }
                if(replaceMaterial==null){
                    ItemStack itemStack = new ItemStack(Material.YELLOW_WOOL);
                    ItemMeta itemMeta = itemStack.getItemMeta();
                    assert itemMeta != null;
                    itemMeta.setDisplayName("§d魔棒工具(等待中)");
                    ArrayList<String> loer = new ArrayList<>();
                    loer.add("§e将需要替换的方块放置到旁边设置栏");
                    loer.add("");
                    loer.add("--------------------------------");
                    loer.add("§g帮助:");
                    loer.add("§g1.激活魔棒工具");
                    loer.add("§g2.放置需要替换的方块         §a<<--当前步骤.");
                    loer.add("§g3.使用工具右键需要替换的方块");
                    itemMeta.setLore(loer);
                    itemStack.setItemMeta(itemMeta);
                    menu.setItem(position,itemStack);
                    return;
                }
                if(!WandFunctionBook.airReplace){
                    if(replaceMaterial.equals(Material.AIR)){
                        ItemStack itemStack = new ItemStack(Material.YELLOW_WOOL);
                        ItemMeta itemMeta = itemStack.getItemMeta();
                        assert itemMeta != null;
                        itemMeta.setDisplayName("§d魔棒工具(等待中)");
                        ArrayList<String> loer = new ArrayList<>();
                        loer.add("§e将需要替换的方块放置到旁边设置栏 (不可以使用空气)");
                        loer.add("");
                        loer.add("--------------------------------");
                        loer.add("§g帮助:");
                        loer.add("§g1.激活魔棒工具");
                        loer.add("§g2.放置需要替换的方块         §a<<--当前步骤.");
                        loer.add("§g3.使用工具右键需要替换的方块");
                        itemMeta.setLore(loer);
                        itemStack.setItemMeta(itemMeta);
                        menu.setItem(position,itemStack);
                        return;
                    }
                }
                if(wandFunctionState!=null){
                    if(!wandFunctionState.isCancel()){
                        ItemStack itemStack = new ItemStack(Material.YELLOW_WOOL);
                        ItemMeta itemMeta = itemStack.getItemMeta();
                        assert itemMeta != null;
                        itemMeta.setDisplayName("§d魔棒工具(正在填充)");
                        ArrayList<String> loer = new ArrayList<>();
                        loer.add("§e点击取消正在进行的填充");
                        loer.add("");
                        loer.add("--------------------------------");
                        loer.add("§g帮助:");
                        loer.add("§g1.激活魔棒工具");
                        loer.add("§g2.放置需要替换的方块         §a<<--当前步骤.");
                        loer.add("§g3.使用工具右键需要替换的方块");
                        itemMeta.setLore(loer);
                        itemStack.setItemMeta(itemMeta);
                        menu.setItem(position,itemStack);
                        ShowFilling = true;
                        return;
                    }
                }
                ItemStack itemStack = new ItemStack(Material.LIME_WOOL);
                ItemMeta itemMeta = itemStack.getItemMeta();
                assert itemMeta != null;
                itemMeta.setDisplayName("§d魔棒工具(可使用)");
                ArrayList<String> loer = new ArrayList<>();
                loer.add("§e使用工具右键方块即可替换周围相同方块");
                loer.add("");
                loer.add("--------------------------------");
                loer.add("§g帮助:");
                loer.add("§g1.激活魔棒工具");
                loer.add("§g2.放置需要替换的方块");
                loer.add("§g3.使用工具右键需要替换的方块   §a<<--当前步骤.");
                itemMeta.setLore(loer);
                itemStack.setItemMeta(itemMeta);
                menu.setItem(position,itemStack);
            }
            @Override
            public void click(InventoryClickEvent e) {
                e.setCancelled(true);
                SelectorImplement selectorImpleme = Selector.getPlayerSelectorimplement(player);
                if(selectorImpleme!=selectorImplement){
                    Selector.setPlayerSelectorimplement((Player) e.getWhoClicked(),selectorImplement);
                    open((Player) e.getWhoClicked(),e.getInventory(),e.getRawSlot());
                    return;
                }
                if(ShowFilling){
                    if(wandFunctionState!=null){
                        wandFunctionState.setCancel(true);
                    }
                    open((Player) e.getWhoClicked(),e.getInventory(),e.getRawSlot());
                    return;
                }
            }
        };
        MenuManger.setSingleClick(49,clickImplement);

        ClickImplement clickImplement1 = new ClickImplement() {
            @Override
            public void click(InventoryClickEvent e) {
                e.setCancelled(true);
                ItemStack itemStack = e.getCursor();
                if(itemStack==null){
                    return;
                }
                Material material = itemStack.getType();
                if(!material.isBlock()){
                    ItemStack itemStack1 = new ItemStack(Material.RED_STAINED_GLASS_PANE);
                    ItemMeta itemMeta = itemStack1.getItemMeta();
                    assert itemMeta != null;
                    itemMeta.setDisplayName("§d此物品不是一个方块");
                    itemStack1.setItemMeta(itemMeta);
                    e.getInventory().setItem(e.getRawSlot(),itemStack1);
                    return;
                }
                replaceMaterial = material;
                MenuManger.open((Player) e.getWhoClicked());
            }
            @Override
            public void open(Player player, Inventory menu, int position) {
                if(replaceMaterial==null){
                    ItemStack itemStack = new ItemStack(Material.GLASS_PANE);
                    ItemMeta itemMeta = itemStack.getItemMeta();
                    assert itemMeta != null;
                    itemMeta.setDisplayName("§d将需要填充的方块放到此处");
                    itemStack.setItemMeta(itemMeta);
                    menu.setItem(position,itemStack);
                    return;
                }
                if(replaceMaterial.equals(Material.AIR)){
                    ItemStack itemStack = new ItemStack(Material.GLASS);
                    ItemMeta itemMeta = itemStack.getItemMeta();
                    assert itemMeta != null;
                    itemMeta.setDisplayName("空气");
                    ArrayList<String> loer = new ArrayList<>();
                    loer.add("§g(将会填充此方块)");
                    itemMeta.setLore(loer);
                    itemStack.setItemMeta(itemMeta);
                    menu.setItem(position,itemStack);
                    return;
                }
                ItemStack itemStack = new ItemStack(replaceMaterial);
                ItemMeta itemMeta = itemStack.getItemMeta();
                assert itemMeta != null;
                ArrayList<String> loer = new ArrayList<>();
                loer.add("§g(将会填充此方块)");
                itemMeta.setLore(loer);
                itemStack.setItemMeta(itemMeta);
                menu.setItem(position,itemStack);
            }
        };
        MenuManger.setSingleClick(46, clickImplement1);
        MenuManger.setSingleClick(47, clickImplement1);
        MenuManger.setSingleClick(48, clickImplement1);
        MenuManger.setSingleClick(50, clickImplement1);
        MenuManger.setSingleClick(51, clickImplement1);
        MenuManger.setSingleClick(52, clickImplement1);

        //挖掘工具
        MenuManger.setSingleClick(53, MiningToolsButton);
        MenuManger.setSingleClick(45, MiningToolsButton);
    }

    @Override
    public void click(InventoryClickEvent e) {
        if(e.getWhoClicked() instanceof Player){
            MenuManger.open((Player) e.getWhoClicked());
        }
    }

    /**
     * 扣除对应的物品
     * @return 成功true 没有物品false
     * */
    public boolean buckle(Material material){
        if(material.equals(Material.AIR)){
            return true;
        }
        for(int i=0;i<45;i++){
            ItemStack itemStack = MenuManger.getItem(i);
            if(itemStack!=null){
                if(itemStack.getType().equals(material)){
                    itemStack.setAmount(itemStack.getAmount()-1);
                    MenuManger.setItem(i,itemStack);
                    return true;
                }
            }
        }
        return false;
    }
}
